package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Message;
import com.example.exception.BusinessException;
import com.example.mapper.MessageMapper;
import com.example.service.MessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Override
    @Transactional
    public boolean sendMessage(Message message) {
        if (message.getReceiverId() == null) {
            throw new BusinessException("接收者ID不能为空");
        }
        message.setStatus(0);  // 设置为未读
        return save(message);
    }

    @Override
    @Transactional
    public boolean batchSendMessage(Message message, Long[] receiverIds) {
        if (receiverIds == null || receiverIds.length == 0) {
            throw new BusinessException("接收者ID列表不能为空");
        }
        
        List<Message> messages = Arrays.stream(receiverIds)
                .map(receiverId -> {
                    Message msg = new Message();
                    msg.setTitle(message.getTitle());
                    msg.setContent(message.getContent());
                    msg.setType(message.getType());
                    msg.setSenderId(message.getSenderId());
                    msg.setSenderName(message.getSenderName());
                    msg.setReceiverId(receiverId);
                    msg.setStatus(0);  // 设置为未读
                    return msg;
                })
                .toList();
        
        return saveBatch(messages);
    }

    @Override
    @Transactional
    public boolean markAsRead(Long id) {
        Message message = getById(id);
        if (message == null) {
            throw new BusinessException("消息不存在");
        }
        message.setStatus(1);  // 设置为已读
        return updateById(message);
    }

    @Override
    @Transactional
    public boolean batchMarkAsRead(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new BusinessException("消息ID列表不能为空");
        }
        
        List<Message> messages = listByIds(Arrays.asList(ids));
        messages.forEach(message -> message.setStatus(1));  // 设置为已读
        return updateBatchById(messages);
    }

    @Override
    @Transactional
    public boolean deleteMessage(Long id) {
        return removeById(id);
    }

    @Override
    @Transactional
    public boolean batchDeleteMessages(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new BusinessException("消息ID列表不能为空");
        }
        return removeByIds(Arrays.asList(ids));
    }

    @Override
    public Page<Message> getMessageList(Integer pageNum, Integer pageSize, Long receiverId, Integer type, Integer status) {
        Page<Message> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
        
        wrapper.eq(Message::getReceiverId, receiverId);
        if (type != null) {
            wrapper.eq(Message::getType, type);
        }
        if (status != null) {
            wrapper.eq(Message::getStatus, status);
        }
        
        wrapper.orderByDesc(Message::getCreateTime);
        return page(page, wrapper);
    }

    @Override
    public Integer getUnreadCount(Long receiverId) {
        LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Message::getReceiverId, receiverId)
                .eq(Message::getStatus, 0);  // 未读状态
        return Math.toIntExact(count(wrapper));
    }
} 